<?php
namespace app\index\service;

use app\index\model\CertCompanyModel;
use app\index\model\CertModel;
use app\index\model\PersonModel;

class CertService extends CommonService
{
    public function get_person_xueli(){
        return ['初中','中专','高中','大专','本科','硕士','博士','研究生','未知'];
    }
    public function get_person_nature(){
        return ['全职','外聘','项目部','临时'];
    }
    public function get_person_state(){
        return ['在职','离职'];
    }
    public function get_person_social(){
        return ['已参保','未参保'];
    }



    //仅非离职人员，离职人员直接为0
    //1、参保开始月份 不为空，且参保结束月份 为空 或 大于等于当前月份，否则直接是0；
    //2、计算 当前月份-参保开始月份+1；
    function get_person_social_month($state, $date1,$date2){
        if($state == '离职'){
            return 0;
        }
        if(!$date1 || $date1 == '0000-00-00'){
            return 0;
        }
        if(!$date2  || $date2 == '0000-00-00' || $date2 > date("Y-m-d")){
            $date2 = date("Y-m");
        }
        if($date1 > $date2){
            return 0;
        }
        if($date2 < date("Y-m")){
            return 0;
        }
        $date1 = date("Y-m-d",strtotime($date1.'-01'));
        $date2 = date("Y-m-d",strtotime($date2.'-01'));
        if(strtotime($date1)>strtotime($date2)){
            $tmp=$date2;
            $date2=$date1;
            $date1=$tmp;
        }
        list($Y1,$m1,$d1)=explode('-',$date1);
        list($Y2,$m2,$d2)=explode('-',$date2);
        $y=$Y2-$Y1;
        $m=$m2-$m1;
        $d=$d2-$d1;
        if($d<0){
            $d+=(int)date('t',strtotime("-1 month $date2"));
            $m--;
        }
        if($m<0){
            $m+=12;
            $y--;
        }
        $mounth = $y*12+$m+1;
        return $mounth;
    }

    public function check_person_name($name, $id=0){
        return !!PersonModel::where(['comid'=>$this->userinfo['comid'],'name'=>$name,'deleted_user'=>0])->where('id','neq', $id)->find();
    }
    public function check_person_idcard_mate($idcard){
        return !!preg_match('/^\d{17}[0-9xX]$/', $idcard);
    }
    public function check_person_idcard_exist($idcard, $id=0){
        return !!PersonModel::where(['idcard'=>$idcard, 'comid'=>$this->userinfo['comid'] ,'deleted_user'=>0])->where('id','neq', $id)->find();
    }
    function get_age_by_idcard($idcard){
        if(!$idcard){
            return '';
        }

        if(!$this->check_person_idcard_mate($idcard)){
            return '';
        }

        $birth_Date = strtotime(substr($idcard, 6, 8));
        $Year = date('Y', $birth_Date);
        $Month = date('m', $birth_Date);
        $Day = date('d', $birth_Date);

        $current_Y = date('Y');
        $current_M = date('m');
        $current_D = date('d');

        $age = $current_Y - $Year;
        if($Month > $current_M || $Month == $current_M && $Day > $current_D){
            $age--;
        }
        return $age;
    }
    public function check_cert_number($number, $id=0){
        return !!CertModel::where(['comid'=>$this->userinfo['comid'],'number'=>$number,'deleted_user'=>0])->where('id','neq', $id)->find();
    }
    public function check_cert_company_number($number, $id=0){
        if(!$id){
            return !!CertCompanyModel::where(['comid'=>$this->userinfo['comid'],'number'=>$number,'deleted_user'=>0])->where('id','neq', $id)->find();
        }else{
            $info = CertCompanyModel::where(['id'=>$id])->find();
            $qita = CertCompanyModel::where(['comid'=>$this->userinfo['comid'],'number'=>$number,'deleted_user'=>0])->where('id','neq', $id)->select()->toArray();
            $exist = false;
            foreach ($qita as $vv){
                if($info['pid'] == 0){
                    if($vv['pid'] != $info['id']){
                        $exist = true;
                        break;
                    }
                }else{
                    if($vv['id'] == $info['pid'] || $vv['pid'] == $info['pid']){

                    }else{
                        $exist = true;
                        break;
                    }
                }
            }
            return $exist;
        }

    }
    public function get_cert_form(){
        return ['纸质原件', '电子件'];
    }
    public function get_cert_state(){
        return ['正常', '转出', '注销', '遗失', '办理中', '停用'];
    }
    public function get_cert_statueuse(){
        return ['空闲', '在用'];
    }
    public function get_cert_goes(){
        return ['在册', '借出'];
    }
    public function get_cert_company_goes(){
        return ['在册', '借出'];
    }
    public function get_cert_company_state(){
        return ['正常', '转出', '注销', '遗失', '办理中', '停用'];
    }
    public function get_cert_company_form(){
        return ['纸质原件', '电子件'];
    }
    public function get_cert_borrow_state(){
        return [
            0 => '待取证',
            1 => '待归还',
            2 => '已归还',
        ];
    }
    public function get_cert_borrow_state_name($state=0){
        $arr = $this->get_cert_borrow_state();
        $str = isset($arr[$state]) ? $arr[$state] : '';
        return $str;
    }
    public function get_cert_borrow_lingtype(){
        return ['快递', '自取',];
    }
    public function get_cert_place(){
        return ['项目经理','项目技术负责人','施工员','安全员','质检员','材料员','造价员','测量员'
            ,'资料员','取样员','机械员','劳务员'];
    }
    public function get_cert_pay_type(){
        return [
            1 => '月付',
            2 => '年付'
        ];
    }
    public function get_cert_pay_type_name($pay_type){
        if($pay_type == 1){
            return '月付';
        }elseif($pay_type == 2){
            return '年付';
        }else{
            return '';
        }
    }
    /**
     * 获取当前年的天数
     * @param $year
     * @return int
     */
    public function cal_days_in_year($year){
        $days=0;
        for($month=1;$month<=12;$month++){
            $days = $days + cal_days_in_month(CAL_GREGORIAN,$month,$year);
        }
        return $days;
    }
    function get_cert_use_months($date1,$date2){
        if(!$date2 || $date2 == '0000-00-00'){
            $date2 = date("Y-m-d");
        }
        if($date1 > $date2 || !$date1 || $date1 == '0000-00-00'){
            return 0;
        }

        $time1 = strtotime($date1);
        $time2 = strtotime($date2);

        $days = (($time2 - $time1) / 86400) / 31;
        $days_ff = floor($days)+0.5;
        $days_cc = ceil($days);

        $days = $days < $days_ff ? $days_ff : $days_cc;
        return $days;
    }





























































}
























